﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using parkMonitor.DataBase;
using System.Configuration;
using parkMonitor.entity;
using System.Configuration;

namespace parkMonitor.server.CoreThread
{
    public class AllotParkingSpace
    {
        //根据车位位置及状态分配目标车位，返回Parking Space的ID
        public Parking_Space MallocParkingSpace(CEntrance pt_Ent, string connectionStr, Command queueCmd)
        {
            DBOperation oper = new DBOperation();
            Dictionary<int, Parking_Space> lps = null;
            lps = oper.GetParkingSpace(connectionStr,queueCmd.garageID);
            double xWeight = Convert.ToDouble(ConfigurationManager.AppSettings["xWeight"]);
            double yWeight = Convert.ToDouble(ConfigurationManager.AppSettings["yWeight"]);
            double zWeight = Convert.ToDouble(ConfigurationManager.AppSettings["zWeight"]);

            Parking_Space rps = new Parking_Space();

            float min_dis = 10000;
            for (int i = 1; i <= lps.Count(); i++)
            {
                float deltaX = (lps[i].parkingSpaceX - pt_Ent.parkingEntX) * (float)xWeight;
                float deltaY = (lps[i].parkingSpaceY - pt_Ent.parkingEntY) * (float)yWeight;
                float deltaZ = (lps[i].parkingSpaceZ - pt_Ent.parkingEntZ) * (float)zWeight;
                float d = (float)System.Math.Sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ);
                if (d < min_dis)
                {
                    min_dis = d;
                    rps = lps[i];
                }

            }
            return rps;
        }
    }

    public class CEntrance
    {
        public int parkingEntX { get; set; }
        public int parkingEntY { get; set; }
        public int parkingEntZ { get; set; }
    }
}
